package ngat.oss.simulation;

import java.util.*;

import ngat.phase2.*;
import ngat.util.*;
import ngat.util.logging.*;

public class BasicDespatcher implements Despatcher {

    /** Candidate generator.*/
    private CandidateGenerator candGen;

    /** Metric rank-selector.*/
    private SelectionHeuristic selector;

    /** Calculates how much time left for group to start in a given window - NOT USED.*/
    private TimingConstraintWindowCalculator tcwCalculator;

    /** Logger.*/
    private LogProxy logger;

    /** Create a BasicDespatcher using provided models.*/
    public BasicDespatcher(CandidateGenerator candGen,
			   SelectionHeuristic selector) {
	this.candGen  = candGen;
	this.selector = selector;
	
	//	this.tcwCalculator  = tcwCalculator;	
	Logger slogger = LogManager.getLogger("SIM");
	logger = new LogProxy("BasicDespatcher", "", slogger);

    }

    /** Returns the metrics of the <i>best</i> group to execute at
     * the specified time under the specified seeing or null if none suitable.
     * @param time The time we want the selection for.
     * @param seeing the seeing at that time.
     */
    public Metric getScheduleItem(long time, EnvironmentSnapshot env) throws Exception {

	logger.method("getScheduleItem(t,Env)").log(2, "Start sweep at: "+ScheduleSimulator.sdf.format(new Date(time)));

	List candidates = candGen.getCandidateMetrics(time, env);

	logger.log(1, ScheduleSimulator.sdf.format(new Date(time))+" Contention= "+candidates.size());

	// could be null	
	Metric best = selector.getBestGroup(candidates);

	return best;

    }

}
